/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package GUI;

import Enum.LoaiNguoiDungEnum;
import dao.NguoiDungDAO;
import java.awt.Component;
import java.util.Date;
import javax.swing.JOptionPane;
import org.apache.log4j.Logger;
import pojo.LoaiNguoiDung;
import pojo.NguoiDung;

/**
 *
 * @author softviet
 */
public class KhachHangGUI extends javax.swing.JDialog {

    /**
     * Creates new form KhachHangGUI
     */
    private NguoiDung nguoiDung;
    private JErrorProvider errorProvider;
    private MyEventListener ButtonLuuClickedListener;
    private static Logger logger = Logger.getLogger(KhachHangGUI.class);
    private NguoiDung oldNguoiDung;

    public KhachHangGUI(java.awt.Frame parent, boolean modal, NguoiDung nguoiDung) {
        super(parent, modal);
        initComponents();
        errorProvider = new JErrorProvider();
        this.nguoiDung = nguoiDung;
        oldNguoiDung = new NguoiDung();
        oldNguoiDung.setCmnd(nguoiDung.getCmnd());
        oldNguoiDung.setDiaChi(nguoiDung.getDiaChi());
        oldNguoiDung.setDienThoai(nguoiDung.getDienThoai());
        oldNguoiDung.setIsDelete(false);
        oldNguoiDung.setLoaiNguoiDung(nguoiDung.getLoaiNguoiDung());
        oldNguoiDung.setNgaySinh(nguoiDung.getNgaySinh());
        oldNguoiDung.setTenNguoiDung(nguoiDung.getTenNguoiDung());
        if (nguoiDung.getId() == 0) {
            this.setTitle("Thêm khách hàng");
        } else {
            this.setTitle("Sửa khách hàng");
            txtCMND.setText(nguoiDung.getCmnd());
            txtDiaChi.setText(nguoiDung.getDiaChi());
            txtDienThoai.setText(nguoiDung.getDienThoai());
            txtTenKhachHang.setText(nguoiDung.getTenNguoiDung());
            dtNgaySinh.setDate(nguoiDung.getNgaySinh());
        }
        SetTabOrder();
        FormatControls();
    }

    private void SetTabOrder() {
        IndexedFocusTraversalPolicy indexedFocusTraversalPolicy = new IndexedFocusTraversalPolicy();
        indexedFocusTraversalPolicy.addIndexedComponents(new Component[]{txtTenKhachHang, txtCMND, txtDienThoai, txtDiaChi, dtNgaySinh, btnLuu, btnThoat});
        setFocusTraversalPolicy(indexedFocusTraversalPolicy);
    }

    private void FormatControls() {
        Utility.FormatNumberStringTextfield(txtCMND);
        Utility.FormatNumberStringTextfield(txtDienThoai);
    }

    private int CheckDataBeforeLuu() {
        int dem = 0;
        errorProvider.clear();
        if (txtTenKhachHang.getText().trim().isEmpty()) {
            errorProvider.setError(txtTenKhachHang, "required");
            dem++;
        }
        if (txtCMND.getText().isEmpty()) {
            errorProvider.setError(txtCMND, "required");
            dem++;
        }
        if (txtDiaChi.getText().trim().isEmpty()) {
            errorProvider.setError(txtDiaChi, "required");
            dem++;
        }
        if (dtNgaySinh.getDate() == null) {
            errorProvider.setError(dtNgaySinh, "required");
            dem++;
        }
        return dem;
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        txtTenKhachHang = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        txtCMND = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        txtDienThoai = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        txtDiaChi = new javax.swing.JTextField();
        jLabel5 = new javax.swing.JLabel();
        dtNgaySinh = new org.jdesktop.swingx.JXDatePicker();
        btnLuu = new javax.swing.JButton();
        btnThoat = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setResizable(false);

        jLabel1.setText("Tên khách hàng");

        jLabel2.setText("CMND");

        jLabel3.setText("Điện thoại");

        jLabel4.setText("Địa chỉ");

        jLabel5.setText("Ngày sinh");

        btnLuu.setText("Lưu");
        btnLuu.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnLuuActionPerformed(evt);
            }
        });

        btnThoat.setText("Thoát");
        btnThoat.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnThoatActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(btnLuu)
                        .addGap(18, 18, 18)
                        .addComponent(btnThoat))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel1)
                            .addComponent(jLabel2)
                            .addComponent(jLabel3))
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(txtTenKhachHang, javax.swing.GroupLayout.DEFAULT_SIZE, 201, Short.MAX_VALUE)
                            .addComponent(txtCMND)
                            .addComponent(txtDienThoai))
                        .addGap(48, 48, 48)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel5)
                            .addComponent(jLabel4))))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(txtDiaChi)
                    .addComponent(dtNgaySinh, javax.swing.GroupLayout.DEFAULT_SIZE, 201, Short.MAX_VALUE))
                .addContainerGap(32, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(txtTenKhachHang, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel4)
                    .addComponent(txtDiaChi, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(txtCMND, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel5)
                    .addComponent(dtNgaySinh, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(txtDienThoai, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnLuu)
                    .addComponent(btnThoat))
                .addContainerGap(19, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnThoatActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnThoatActionPerformed
        // TODO add your handling code here:
        dispose();
    }//GEN-LAST:event_btnThoatActionPerformed

    private void btnLuuActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnLuuActionPerformed
        // TODO add your handling code here:
        if (CheckDataBeforeLuu() > 0) {
            return;
        }
        nguoiDung.setCmnd(txtCMND.getText());
        nguoiDung.setDiaChi(txtDiaChi.getText().trim());
        nguoiDung.setDienThoai(txtDienThoai.getText());
        nguoiDung.setIsDelete(false);
        nguoiDung.setLoaiNguoiDung(new LoaiNguoiDung(LoaiNguoiDungEnum.KhachHang.getCode()));
        nguoiDung.setNgaySinh(dtNgaySinh.getDate());
        nguoiDung.setTenNguoiDung(txtTenKhachHang.getText().trim());
        if (nguoiDung.getId() == 0) {
            try {
                if (ButtonLuuClickedListener == null) {
                    return;
                }
                int id = NguoiDungDAO.Them(nguoiDung);
                nguoiDung.setId(id);
                JOptionPane.showMessageDialog(null, "Thêm thành công");
                ButtonLuuClickedListener.myEventOccured(nguoiDung);
                dispose();
            } catch (Exception ex) {
                logger.error(new Date().toString(), ex);
                JOptionPane.showMessageDialog(this, ex.getMessage());
            }
        } else {
            try {
                NguoiDungDAO.Sua(nguoiDung);
                JOptionPane.showMessageDialog(this, "Cập nhật thành công");
                dispose();
            } catch (Exception ex) {
                nguoiDung.setCmnd(oldNguoiDung.getCmnd());
                nguoiDung.setDiaChi(oldNguoiDung.getDiaChi());
                nguoiDung.setDienThoai(oldNguoiDung.getDienThoai());
                nguoiDung.setIsDelete(false);
                nguoiDung.setLoaiNguoiDung(oldNguoiDung.getLoaiNguoiDung());
                nguoiDung.setNgaySinh(oldNguoiDung.getNgaySinh());
                nguoiDung.setTenNguoiDung(oldNguoiDung.getTenNguoiDung());
                logger.error(new Date().toString(), ex);
                JOptionPane.showMessageDialog(this, ex.getMessage());
            }
        }
    }//GEN-LAST:event_btnLuuActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnLuu;
    private javax.swing.JButton btnThoat;
    private org.jdesktop.swingx.JXDatePicker dtNgaySinh;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JTextField txtCMND;
    private javax.swing.JTextField txtDiaChi;
    private javax.swing.JTextField txtDienThoai;
    private javax.swing.JTextField txtTenKhachHang;
    // End of variables declaration//GEN-END:variables

    /**
     * @param ButtonLuuClickedListener the ButtonLuuClickedListener to set
     */
    public void setButtonLuuClickedListener(MyEventListener ButtonLuuClickedListener) {
        this.ButtonLuuClickedListener = ButtonLuuClickedListener;
    }
}
